<?php
#
#
# Copyright TEDECO, 2009. http://tedeco.fi.upm.es
#
# This file is part of kumenya.
#
# Kumenya is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kumenya is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
?>
<?php
// Editable variables.
$Host = "localhost";
$User = "kumenyaapp";
$Password = "EfJ7i49c3v11";
$DBName = "kumenya_matriculacion";

$career = $_POST["career"];
$year = $_POST["year"];

if ($year != "") $select_students_year_list = "SELECT DISTINCT personas.CodigoPersona, personas.Nombre, personas.Apellidos FROM ((personas RIGHT JOIN alumnos ON alumnos.Uid = personas.Uid) RIGHT JOIN matriculas on matriculas.UidAlumno = alumnos.Uid) RIGHT JOIN  matriculasasignaturas ON matriculasasignaturas.UidMatricula = matriculas.Uid WHERE (matriculas.anio = " . $year . ") AND (matriculasasignaturas.UidAsignatura = ANY (SELECT Uid FROM asignaturas WHERE Carrera = " . $career . ")) ORDER BY personas.CodigoPersona"; 
else $select_students_year_list = "SELECT DISTINCT personas.CodigoPersona, personas.Nombre, personas.Apellidos FROM ((personas RIGHT JOIN alumnos ON alumnos.Uid = personas.Uid) RIGHT JOIN matriculas on matriculas.UidAlumno = alumnos.Uid) RIGHT JOIN  matriculasasignaturas ON matriculasasignaturas.UidMatricula = matriculas.Uid WHERE (matriculasasignaturas.UidAsignatura = ANY (SELECT Uid FROM asignaturas WHERE Carrera = " . $career . ")) ORDER BY personas.CodigoPersona";

$select_career = "SELECT carrera FROM carreras WHERE Uid = ".$career;
// End editable variables.

function Print_table($res)
{
   $rows = mysql_num_rows($res);

   if (!$rows) printf("<div align=\"center\"><H2>Table is empty.</H2></div>");
   else
   {
      printf("<TABLE BORDER=1 CELLSPACING=1 CELLPADDING=1>\n");
      printf("<tr>");
      printf("<th>index</th>");
      $numberfields = mysql_num_fields($res);
      for ($i=0; $i<$numberfields; $i++)// Print head.
      {
          printf("<th>%s</th>", mysql_field_name($res, $i));
      }
      printf("</tr>\n");
      
      $j = 1;
      while ($row = mysql_fetch_row($res))
      {
         printf("<tr>");
         printf("<th>%d</th>", $j++);
         for ($i=0; $i<$numberfields; $i++)
         {
            if ($row[$i] != "") printf("<td>%s</td>", $row[$i]);
            else printf("<td>&nbsp;</td>");
         }
         printf("</tr>\n");
      }
      printf("</TABLE>");
   }
}

// Connect to DB
$link = mysql_connect ($Host, $User, $Password) or die('Could not connect: ' . mysql_error());
mysql_select_db($DBName) or die('Could not select database');

$result = mysql_query($select_career);
$row = mysql_fetch_row($result);
$career = $row[0];

include("../commons/clases.php");
include("../commons/www.php");
include(Accion . "students_list_aux.php");

Interfaz::Cabecera(_("Student") . " " . _(" ") . " " . _("List"));

Interfaz::AbrirCuerpo(FALSE);

Interfaz::ProximaPantalla ();

Interfaz::Botones ();
Interfaz::Formulario (Pagina. "students_list.php","back");
Interfaz::BotonDeEnvio(_("Back"));

$pattern = "/^[0-9]{4}$/";
if (!preg_match($pattern, $year)) echo '<br />Year not valid.<br /><br />';
else 
{
    echo
    '
        <input type="hidden" name="query" value="'.$select_students_year_list.'" />
        <input type="hidden" name="title" value="Students List of '.$career.', '.$year.'" />
        <input type="hidden" name="file_name" value="Students_List" />
    '; 
    Interfaz::BotonNavegacion (_("Export to excel"), Pagina . "export_xls.php");
    Interfaz::CerrarBotones ();
    Interfaz::CerrarFormulario ();
    
    $result = mysql_query($select_students_year_list);
    
    print_table ($result);    
}

Interfaz::Fin();

exit;
?>
